package com.culturalCenter.dataCenter.entity;

import com.culturalCenter.dataCenter.customEntity.CustomAdmin;
import com.culturalCenter.dataCenter.entity.user.Role;
import com.culturalCenter.dataCenter.entity.user.UserRole;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created MR.WU 2020/06/11
 */
public class JwtUser implements UserDetails {

    private String id;
    private String username;
    private String password;
    private Collection<? extends GrantedAuthority> authorities;
    /**
     * 自定义增加属性
     */
    private List<UserRole> userRoles;
    private List<Role> tbRoles;
    private String realname;
    private String mobile;
    private String picture;
    private List<Map<String, String>> roleUrl;


    public JwtUser() {
    }

    // 写一个能直接使用user创建jwtUser的构造器
    public JwtUser(CustomAdmin user) {
        id = user.getUser().getId();
        username = user.getUser().getNickName();
        password = user.getUser().getPassword();
        authorities = getAuthorities(user);
        userRoles = user.getUserRoles();
        tbRoles = user.getRoles();
        realname = user.getUser().getName();
        mobile = user.getUser().getPhone();
        picture = user.getUser().getPhoto();
//        authorities = Collections.singleton(new SimpleGrantedAuthority(user.getRoles()));
    }

    /**
     * 授权权限
     */
    public Collection<? extends GrantedAuthority> getAuthorities(CustomAdmin user) {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (Role role : user.getRoles()) {
            authorities.add(new SimpleGrantedAuthority(role.getRoleCode()));
        }
        return authorities;
    }

    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }

    public List<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(List<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

    public List<Role> getTbRoles() {
        return tbRoles;
    }

    public void setTbRoles(List<Role> tbRoles) {
        this.tbRoles = tbRoles;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public List<Map<String, String>> getRoleUrl() {
        return roleUrl;
    }

    public void setRoleUrl(List<Map<String, String>> roleUrl) {
        this.roleUrl = roleUrl;
    }

    @Override
    public String toString() {
        return "{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", authorities=" + authorities +
                ", realname='" + realname + '\'' +
                ", mobile='" + mobile + '\'' +
                '}';
    }

}
